package cn.jimmie.lib.qlog.support;


import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

public class AlNetWork {
    private static final String NETWORK_TYPE_WIFI = "WIFI";
    private static final String NETWORK_TYPE_UNKNOWN = "";
    private static final String NETWORK_TYPE_UNCONNECTED = "unconnected";
    private static final String NETWORK_TYPE_2_G = "2G";
    private static final String NETWORK_TYPE_3_G = "3G";
    private static final String NETWORK_TYPE_4_G = "4G";
    private static final String NETWORK_TYPE_5_G = "5G";

    @SuppressWarnings("deprecation")
    public static String getNetworkType(Context ctx) {
        if (ctx == null) return NETWORK_TYPE_UNKNOWN;
        try {
            ConnectivityManager connMgr = (ConnectivityManager) ctx.getApplicationContext()
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connMgr == null) return NETWORK_TYPE_UNCONNECTED;
            NetworkInfo info = connMgr.getActiveNetworkInfo();
            if (info == null || !info.isAvailable())
                return NETWORK_TYPE_UNCONNECTED;

            info = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (null != info) {
                NetworkInfo.State state = info.getState();
                if (null != state)
                    if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
                        return NETWORK_TYPE_WIFI;
                    }
            }

            info = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (null != info && info.getState() != null) {
                if (info.getState() == NetworkInfo.State.CONNECTED || info.getState() == NetworkInfo.State.CONNECTING) {
                    switch (info.getSubtype()) {
                        case TelephonyManager.NETWORK_TYPE_GPRS:
                        case TelephonyManager.NETWORK_TYPE_CDMA:
                        case TelephonyManager.NETWORK_TYPE_EDGE:
                        case TelephonyManager.NETWORK_TYPE_1xRTT:
                        case TelephonyManager.NETWORK_TYPE_IDEN:
                            return NETWORK_TYPE_2_G;
                        case TelephonyManager.NETWORK_TYPE_EVDO_A:
                        case TelephonyManager.NETWORK_TYPE_UMTS:
                        case TelephonyManager.NETWORK_TYPE_EVDO_0:
                        case TelephonyManager.NETWORK_TYPE_HSDPA:
                        case TelephonyManager.NETWORK_TYPE_HSUPA:
                        case TelephonyManager.NETWORK_TYPE_HSPA:
                        case TelephonyManager.NETWORK_TYPE_EVDO_B:
                        case TelephonyManager.NETWORK_TYPE_EHRPD:
                        case TelephonyManager.NETWORK_TYPE_HSPAP:
                            return NETWORK_TYPE_3_G;
                        case TelephonyManager.NETWORK_TYPE_LTE:
                            return NETWORK_TYPE_4_G;
                        case TelephonyManager.NETWORK_TYPE_NR:
                            return NETWORK_TYPE_5_G;
                        default:
                            String subTypeName = info.getSubtypeName();
                            if (subTypeName.equalsIgnoreCase("TD-SCDMA")
                                    || subTypeName.equalsIgnoreCase("WCDMA")
                                    || subTypeName.equalsIgnoreCase("CDMA2000")) {
                                return NETWORK_TYPE_3_G;
                            } else {
                                return NETWORK_TYPE_UNKNOWN;
                            }
                    }
                }
            }
        } catch (Throwable ignored) {
        }

        return NETWORK_TYPE_UNCONNECTED;
    }
}
